package com.llf.nettybook2.chapter15;

import java.nio.ByteBuffer;

/**
 * Jdk ByteBuffer 用法
 * @author xi.yang
 * @create 2019-03-05 13:44
 **/
public class ByteBufferTest {
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(88);
        String value = "Netty权威指南";
        buffer.put(value.getBytes());
        /*
        因为ByteBuffer只有一个位置指针用于读写操作
        每次读写的时候必须调用flip或者clear,否则功能会出错
        */
        buffer.flip();
        byte[] vArray = new byte[buffer.remaining()];
        buffer.get(vArray);
        String decodeValue = new String(vArray);
        System.out.println(decodeValue);
    }
}
